package by.pavlyukevich.paymentsystem.panel;

import javax.inject.Inject;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.hibernate.LazyInitializationException;

import by.pavlyukevich.paymentsystem.add.ErrorEvent;
import by.pavlyukevich.paymentsystem.add.PanelUpdateEvent;
import by.pavlyukevich.paymentsystem.dataaccess.filter.AccountFilter;
import by.pavlyukevich.paymentsystem.datamodel.Account;
import by.pavlyukevich.paymentsystem.datamodel.user.Contractor;
import by.pavlyukevich.paymentsystem.datamodel.user.Individual;
import by.pavlyukevich.paymentsystem.datamodel.user.UserAccount;
import by.pavlyukevich.paymentsystem.services.AccountService;
import by.pavlyukevich.paymentsystem.services.user.UserAccountService;

public class AccountEditPanel extends Panel {
	@Inject
	AccountService accountService;
	@Inject
	UserAccountService userAccountService;

	public AccountEditPanel(String id, Account accountIn, Panel respPanel) {
		super(id);
		if (accountIn.getId() != null) {
			try {
				accountIn.getUserAccount().getIndividual();
			} catch (LazyInitializationException e) {
				accountIn = accountService.getList(new AccountFilter().setAccount(accountIn))
						.get(0);
			}
		}
		Account account = accountIn;

		setOutputMarkupId(true);
		Form<Account> form = new Form<Account>("form", new CompoundPropertyModel<Account>(account));
		add(form);

		form.add(new TextField<String>("accountNumber").add(new PropertyValidator<String>()));
		form.add(new TextField<String>("balance").add(new PropertyValidator<String>()));
		form.add(new CheckBox("isBlocked").add(new PropertyValidator<Boolean>()));

		form.add(new DropDownChoice<UserAccount>("owner", new PropertyModel<UserAccount>(account,
				"userAccount"), userAccountService.getList(), new IChoiceRenderer<UserAccount>() {
			@Override
			public Object getDisplayValue(UserAccount userAccount) {
				Individual individual = userAccount.getIndividual();
				Contractor contractor = userAccount.getContractor();
				return (individual != null) ? individual.getFamily() + " " + individual.getName()
						: contractor.getName();
			}

			@Override
			public String getIdValue(UserAccount object, int index) {
				return Integer.toString(index);
			}
		}).add(new PropertyValidator<UserAccount>()));

		form.add(new AjaxSubmitLink("save")

		{
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				accountService.saveOrUpdate(account);
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target, respPanel));
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				send(getPage(), Broadcast.BREADTH, new ErrorEvent(target));
			}
		});

	}
}
